﻿using DevTool.Command;
using MySqlX.XDevAPI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;

namespace DevTool.Contraner
{
    public partial class MySqlModelsControl : UserControl
    {
        private MySQLDB mySQLDB;

        
        public MySqlModelsControl()
        {
            mySQLDB = new MySQLDB();
            InitializeComponent();

            Init();
        }

        public void Init()
        {

            var tbname = mySQLDB.GetDBTabeName(mySQLDB.GetDBName());
            this.cb_TableName.ValueMember = "table_name";
            this.cb_TableName.DisplayMember = "table_name";
            this.cb_TableName.DataSource = tbname;


        }
        private void bt_Init_Click(object sender, EventArgs e)
        {
            Init();
        }

        private void bt_Create_Click(object sender, EventArgs e)
        {
            this.rt_Context.Clear();
            var lang = this.cb_lang.Text.Trim();
            var tablename = this.cb_TableName.Text.Trim();
            var dbname = mySQLDB.GetDBName();
            if (!string.IsNullOrEmpty(tablename))
            {               
                var coluns = mySQLDB.Getcolnumes(tablename);
                if (coluns != null)
                {
                    if (lang == "C#")
                    {
                        StringBuilder sb = new StringBuilder();
                        sb.AppendLine("using System;");
                        sb.AppendLine("using System.Collections.Generic;");
                        sb.AppendLine("using System.Data;");
                        sb.AppendLine("using System.Linq;");
                        sb.AppendLine("using System.Text;");
                        sb.AppendLine("namespace " + dbname + " { ");
                        sb.AppendLine("public class " + tablename + " {");
                        foreach (DataColumn dc in coluns.Columns)
                        {
                            if (dc.DataType.Name.Contains("Guid") || dc.DataType.Name.Contains("DateTime"))
                            {
                                sb.AppendLine("  public " + dc.DataType.Name + "? " + dc.ColumnName + " { get; set; }");
                            }
                            else
                                sb.AppendLine("  public " + dc.DataType.Name + " " + dc.ColumnName + " { get; set; }");
                        }

                        sb.AppendLine("   }");
                        sb.AppendLine(" }");
                        this.rt_Context.Text = sb.ToString();
                    }
                    else
                    {
                        StringBuilder sb = new StringBuilder();

                        sb.AppendLine($"package {dbname}.models;");
                        sb.AppendLine("import java.math.BigDecimal;");
                        sb.AppendLine("import lombok.Data;");
                        sb.AppendLine("@Data");
                        sb.AppendLine("public class " + tablename + " {");
                        foreach (DataColumn dc in coluns.Columns)
                        {

                            if (dc.DataType.Name.Contains(nameof(Single)))
                            {

                                sb.AppendLine("  private " + "float " + dc.ColumnName + " ;");
                            }
                            else
                            if (dc.DataType.Name.Contains("DateTime"))
                            {
                                sb.AppendLine("  private " + "Date " + dc.ColumnName + " ;");
                            }
                            else
                            if (dc.DataType.Name.Contains(nameof(Int16)))
                            {
                                sb.AppendLine("  private " + "short " + dc.ColumnName + " ;");
                            }
                            else
                            if (dc.DataType.Name.Contains(nameof(Int64)))
                            {
                                sb.AppendLine("  private " + "long " + dc.ColumnName + " ;");
                            }
                            else
                            if (dc.DataType.Name.Contains(nameof(Int32)))
                            {
                                sb.AppendLine("  private " + "int " + dc.ColumnName + " ;");
                            }
                            else
                            if (dc.DataType.Name.Contains("Decimal"))
                            {
                                sb.AppendLine("  private " + "BigDecimal " + dc.ColumnName + " ;");
                            }
                            else

                            if (dc.DataType.Name.Contains("bool"))
                            {
                                sb.AppendLine("  private " + "boolean " + dc.ColumnName + " ;");
                            }
                            else
                            {
                                sb.AppendLine("  private " + dc.DataType.Name + " " + dc.ColumnName + " ;");
                            }
                        }

                        sb.AppendLine(" }");
                        this.rt_Context.Text = sb.ToString();
                    }

                }
            }

        }

        private void bt_Save_Click(object sender, EventArgs e)
        {
            var classname = this.cb_TableName.Text.Trim();
            var lang = this.cb_lang.Text.Trim();
            this.saveFileDialogModel.Filter = "C#|*.cs|JAVA|*.java|所有|*.*";
            this.saveFileDialogModel.RestoreDirectory = true;
            if (lang == "C#")
            {
                this.saveFileDialogModel.FileName = classname + ".cs";
            }
            else if (lang == "JAVA")
            {
                this.saveFileDialogModel.FileName = classname + ".java";
            }
            else
            {
                this.saveFileDialogModel.FileName = classname + ".txt";
            }

            if (saveFileDialogModel.ShowDialog() == DialogResult.OK)
            {
                var path = saveFileDialogModel.FileName;
                try
                {
                    using (var fs = new FileStream(path, FileMode.Append))
                    {
                        StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);
                        sw.Write(this.rt_Context.Text);
                        sw.Close();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }



            }
        }
    }
}
